package com.example.ponycui_home.svgaplayer;

import com.opensource.svgaplayer.*;
import com.opensource.svgaplayer.iml.ParseCompletion;
import com.opensource.svgaplayer.utils.log.LogUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;

/**
 * ex.
 *
 * @author wl
 * @since 2021-06-22
 */
public class AnimationFromClickAbility extends Ability {
    SVGAImageView animationView = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.getIntColor("#FF000000"));
        setUIContent(ResourceTable.Layout_ability_animation_from_click);
        DirectionalLayout root = (DirectionalLayout)findComponentById(ResourceTable.Id_root);
        animationView = new SVGAImageView(this);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbPalette.WHITE);
        animationView.setImageElement(shapeElement);

        animationView.setOnAnimKeyClickListener(new SVGAClickAreaListener() {
            @Override
            public void onClick( String clickKey) {

                new ToastDialog(getContext()).setText(clickKey).setCornerRadius(50).setSize(300,100).setOffset(0,400).show();
            }
        });
        loadAnimation();
        root.addComponent(animationView, ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_PARENT);
    }

    @Override
    protected void onStop() {
        super.onStop();
        animationView.stopAnimation(true);
    }

    private void loadAnimation() {
        SVGAParser.shareParser().decodeFromAssets("MerryChristmas.svga",new ParseCompletion() {
            @Override
            public void onComplete( SVGAVideoEntity videoItem) {
                SVGADynamicEntity dynamicEntity = new SVGADynamicEntity();
                dynamicEntity.setClickArea("img_10");
                LogUtil.loge("00000000", "进来了"  +(videoItem ) );
                animationView.setVideoItem(videoItem,dynamicEntity);
                animationView.startAnimation();
            }
            @Override
            public void onError() {

            }
        });
    }
}
